home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / stonehenge / Color.h < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  190 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #ifndef COLOR_H
  39. #define COLOR_H
  40.  
  41. class Color {
  42.  public:
  43.   inline Color() {};
  44.   inline Color(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0);
  45.   
  46.   inline Color& operator=(GLfloat *a);
  47.   inline Color& operator=(GLfloat a);
  48.   inline Color& operator+(Color a);
  49.   inline Color& operator+=(Color a);
  50.   inline Color& operator*(Color a);
  51.   inline Color& operator*(GLfloat a);
  52.   inline Color& operator*=(Color a);
  53.   inline Color& operator*=(GLfloat *a);
  54.   inline Color& operator*=(GLfloat a);
  55.  
  56.   inline GLfloat& operator[](int index);
  57.  
  58.   inline Color clamp();
  59.   
  60.   inline void glcolor();
  61.  
  62.   inline void print();
  63.   inline void print(const char *format);
  64.  
  65.   GLfloat c[4];
  66. };
  67.  
  68. const Color white(1., 1., 1., 1.), black(0., 0., 0., 1.);
  69. const Color red(1, 0, 0), green(0, 1, 0), blue(0, 0, 1);
  70.  
  71. inline Color::Color(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
  72. {
  73.   c[0] = r;
  74.   c[1] = g;
  75.   c[2] = b;
  76.   c[3] = a;
  77. }
  78.  
  79. inline Color& Color::operator=(GLfloat a)
  80. {
  81.   c[0] = c[1] = c[2] = c[3] = a;
  82.   return *this;
  83. }
  84.  
  85. inline Color& Color::operator=(GLfloat *a)
  86. {
  87.   c[0] = a[0];
  88.   c[1] = a[1];
  89.   c[2] = a[2];
  90.   c[3] = a[3];
  91.   return *this;
  92. }
  93.  
  94. inline Color& Color::operator+(Color a) 
  95. {
  96.   Color val;
  97.   val.c[0] = c[0] + a.c[0];
  98.   val.c[1] = c[1] + a.c[1];
  99.   val.c[2] = c[2] + a.c[2];
  100.   val.c[3] = c[3] + a.c[3];
  101.   return val;
  102. }
  103.  
  104. inline Color& Color::operator+=(Color a)
  105. {
  106.   c[0] += a.c[0];
  107.   c[1] += a.c[1];
  108.   c[2] += a.c[2];
  109.   c[3] += a.c[3];
  110.   return *this;
  111. }
  112.  
  113. inline Color& Color::operator*(Color a)
  114. {
  115.   Color val;
  116.   val.c[0] = c[0] * a.c[0];
  117.   val.c[1] = c[1] * a.c[1];
  118.   val.c[2] = c[2] * a.c[2];
  119.   val.c[3] = c[3] * a.c[3];
  120.   return val;
  121. }
  122.  
  123. inline Color& Color::operator*(GLfloat a)
  124. {
  125.   Color val;
  126.   val.c[0] = c[0] * a;
  127.   val.c[1] = c[1] * a;
  128.   val.c[2] = c[2] * a;
  129.   val.c[3] = c[3] * a;
  130.   return val;
  131. }
  132.  
  133. inline Color& Color::operator*=(Color a)
  134. {
  135.   c[0] *= a.c[0];
  136.   c[1] *= a.c[1];
  137.   c[2] *= a.c[2];
  138.   return *this;
  139. }
  140.  
  141. inline Color& Color::operator*=(GLfloat *a) 
  142. {
  143.   c[0] *= a[0];
  144.   c[1] *= a[1];
  145.   c[2] *= a[2];
  146.   return *this;
  147. }
  148.  
  149. inline Color& Color::operator*=(GLfloat a)
  150. {
  151.   c[0] *= a;
  152.   c[1] *= a;
  153.   c[2] *= a;
  154.   c[3] *= a;
  155.   
  156.   return *this;
  157. }
  158.  
  159. inline GLfloat& Color::operator[](int index)
  160. {
  161.   return c[index];
  162. }
  163.  
  164. inline Color Color::clamp()
  165. {
  166.   Color val;
  167.   val.c[0] = c[0] < 0.0 ? 0.0 : (c[0] > 1.0 ? 1.0 : c[0]);
  168.   val.c[1] = c[1] < 0.0 ? 0.0 : (c[1] > 1.0 ? 1.0 : c[1]);
  169.   val.c[2] = c[2] < 0.0 ? 0.0 : (c[2] > 1.0 ? 1.0 : c[2]);
  170.   val.c[3] = c[3] < 0.0 ? 0.0 : (c[3] > 1.0 ? 1.0 : c[3]);
  171.   return val;
  172. }
  173.  
  174. inline void Color::glcolor()
  175. {
  176.   glColor4fv(c);
  177. }
  178.  
  179. inline void Color::print()
  180. {
  181.   print("%f %f %f %f\n");
  182. }
  183.  
  184. inline void Color::print(const char *format)
  185. {
  186.   printf(format, c[0], c[1], c[2], c[3]);
  187. }
  188.  
  189. #endif
  190.